package conch.yaoms;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import conch.yaoms.jni.MemoryWatcher;
import conch.yaoms.jni.ProcessItem;

public class MainFrame extends JFrame implements ListSelectionListener {

	private static final long serialVersionUID = -7615654549862317641L;
	private Bot bot;
	private JTextArea logArea;

	private MemoryWatcher watcher;
	private ArrayList<ProcessItem> processItems;
	private JButton startButton;
	private JButton stopButton;

	public MainFrame(String title, MemoryWatcher watcher) {
		super(title);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setSize(300, 500);

		this.watcher = watcher;
		processItems = watcher.getProcessList();
		JList proc_list = new JList(processItems.toArray());
		proc_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		proc_list.setCellRenderer(new ProcessListRenderer());
		proc_list.addListSelectionListener(this);

		JScrollPane listScrollPane = new JScrollPane(proc_list);
		listScrollPane.setPreferredSize(new Dimension(300, 70));

		JPanel funcPanel = new JPanel();
		funcPanel.setPreferredSize(new Dimension(290, 70));
		funcPanel.setBorder(BorderFactory.createTitledBorder("机器人控制"));

		startButton = new JButton("开始");
		stopButton = new JButton("停止");

		startButton.setEnabled(false);
		stopButton.setEnabled(false);

		startButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				bot.go();
				stopButton.setEnabled(true);
				startButton.setEnabled(false);
			}
		});

		stopButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				bot.pause();
				stopButton.setEnabled(false);
				startButton.setEnabled(true);
			}
		});

		funcPanel.add(startButton);
		funcPanel.add(stopButton);

		JPanel logPanel = new JPanel();
		logPanel.setPreferredSize(new Dimension(290, 310));
		logPanel
				.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

		logArea = new JTextArea();
		logArea.setEditable(false);
		logArea.setLineWrap(true);
		// logArea.setFont(new Font("WenQuanYi Micro Hei Mono", Font.PLAIN,
		// 12));

		JScrollPane jScrollPane = new JScrollPane(logArea);

		jScrollPane.setPreferredSize(new Dimension(
				logPanel.getPreferredSize().width - 5, logPanel
						.getPreferredSize().height - 10));

		logPanel.add(jScrollPane);

		JPanel container = new JPanel();
		container.setPreferredSize(new Dimension(300, 500));
		container.add(listScrollPane);
		container.add(funcPanel);
		container.add(logPanel);

		add(container);
	}

	public void append(String message) {
		logArea.append(message);
		logArea.setCaretPosition(logArea.getText().length());
	}

	public void setBot(Bot bot) {
		this.bot = bot;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (!e.getValueIsAdjusting()) {
			JList list = (JList) e.getSource();
			Object[] selectionValues = list.getSelectedValues();
			for (int i = 0, n = selectionValues.length; i < n; i++) {
				watcher.setProcessId(((ProcessItem)selectionValues[i]).processId);
				startButton.setEnabled(true);
				list.setEnabled(false);
			}
		}
	}
}
